package com.ultra.template.parse;

import com.ultra.enums.ArgumentTypeEnum;
import com.ultra.exception.UnableResolveTypeException;
import com.ultra.exception.UnknownTypeException;
import com.ultra.template.content.Argument;
import com.ultra.template.content.Pairs;
import sun.reflect.generics.reflectiveObjects.ParameterizedTypeImpl;

import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.ParameterizedType;
import java.lang.reflect.Type;


/**
 * @author yinger
 * @description TODO
 * @date 2024/12/30
 **/
public final class MapParser {
    public static void _fromMap(Argument argument) throws UnableResolveTypeException, ClassNotFoundException, InvocationTargetException, NoSuchMethodException, InstantiationException, IllegalAccessException, UnknownTypeException {
        argument.setIThCollection(Parser.times[argument.getDepth()]++);
        argument.setArgumentType(ArgumentTypeEnum.MAP);
        // 获取集合的泛型信息
        Type type = argument.getType();
        argument.setClassName(((ParameterizedTypeImpl) type).getRawType().getName());
        // 递进前先设置Type
        if (argument.getChild() == null) {
            argument.setChild(new Argument[]{null, null});
        }

        if (argument.getChild()[0] == null) {
            argument.getChild()[0] = new Argument();
        }

        if (argument.getChild()[1] == null) {
            argument.getChild()[1] = new Argument();
        }
        Type[] types = ((ParameterizedType) type).getActualTypeArguments();
        Parser.flatteningMap.put(new Pairs(argument.getIThArgument(),argument.getDepth(),argument.getIThCollection()),argument);

        argument.getChild()[0].setType(types[0]);
        argument.getChild()[0].setIThArgument(argument.getIThArgument());
        argument.getChild()[0].setDepth(argument.getDepth() + 1);
        Parser.parserDispatcher(argument.getChild()[0]);

        argument.getChild()[1].setType(types[1]);
        argument.getChild()[1].setIThArgument(argument.getIThArgument());
        argument.getChild()[1].setDepth(argument.getDepth() + 1);
        Parser.parserDispatcher(argument.getChild()[1]);
    }
}
